// APP
if (window.plus) {
    searchPageReady();
} else {
    document.addEventListener('plusready', searchPageReady, false);
}

// Web
$(function () {
    if (AGENT_CLIENT.WEB) {
        searchPageReady();
    }
});

// 初始化方法
function searchPageReady() {
    var searchPage = function () {
        this.init();
    };
    searchPage.prototype = {
        // 初始化
        init: function () {
            this.bindEvent();
        },

        // 绑定事件
        bindEvent: function () {
            var model = this;
            // 回退
            $("#btncloseseahpage").on("click", function () {
                $("#searchtext").blur();
                parent.closeSearchPage();
            });

            // 是否显示清空按钮
            $("#searchtext").on("keyup", function () {
                if ($(this).val().length > 0) {
                    $("#btnclearsearch i").show();
                } else {
                    $("#btnclearsearch i").hide();
                }
            });

            // 清空搜索条件
            $("#btnclearsearch i").on("click", function () {
                $("#searchtext").val("");
                $(this).hide();
            });

            // 搜索
            $("#btnsearch").on("click", function () {
                var searchword = $("#searchtext").val();
                setSeachHistoryStorage(searchword);

                model.search(searchword);
            });

            // 历史纪录
            $("#searchhistorylist").on("click", "p[name=historysearch]", function () {
                var searchword = $(this).text();

                setSeachHistoryStorage(searchword);
                model.search(searchword);
            });

            // 删除历史纪录
            $("#searchhistorylist").on("click", "p[name=delhistory]", function () {
                var searchtext = $(this).parent().find('p[name=historysearch]').text();
                var list = JSON.parse(localStorage.getItem(STORAGE_MANAGER.ordersearchhistory));
                var itemindex = list.indexOf(searchtext);
                if (itemindex > -1) {
                    list.splice(itemindex, 1);
                }
                localStorage.setItem(STORAGE_MANAGER.ordersearchhistory, JSON.stringify(list));

                $(this).parent().remove();
            });

            $("#searchtext").on("keydown", function (e) {
                e = e || event;
                if (e.keyCode == 13) {  //判断是否单击的enter按键(回车键)
                    var searchword = $("#searchtext").val();
                    setSeachHistoryStorage(searchword);

                    model.search(searchword);
                }
            });

            function setSeachHistoryStorage(searchtext) {
                searchtext = searchtext.trim();
                $("#searchtext").val(searchtext);
                $("#searchtext").blur();
                if (searchtext == "") {
                    return;
                }
                var searchhistory = getLocalStorage(STORAGE_MANAGER.ordersearchhistory);
                var list = [];
                if (searchhistory) {
                    list = searchhistory;
                }
                var itemindex = list.indexOf(searchtext);
                if (itemindex > -1) {
                    list.splice(itemindex, 1);
                }
                list.unshift(searchtext);
                setLocalStorage(STORAGE_MANAGER.ordersearchhistory, list);
            }
        },

        // 选择条件后回掉函数
        search: function (searchword) {
            parent.search(searchword);
            parent.closeSearchPage();
        }
    };

    var _searchPage = new searchPage();
}


function show(searchword) {
    if (!_searchPageInit) {
        searchPageReady();
    }
    setTimeout(function () {
        $("#searchtext").focus();
    }, 300);

    $("#searchtext").val(searchword);
}

// 历史纪录
function loadSearchHistory() {
    var list = getLocalStorage(STORAGE_MANAGER.ordersearchhistory);
    if (!list) {
        return;
    }
    if (list.length > 10) {
        list.splice(10);
    }
    var html = [];
    for (var i = 0; i < list.length; i++) {
        html.push("<div class='mbbottom lsjl'>");
        html.push("<p class='lsjlny floatl heizi' name='historysearch'>" + list[i] + "</p>");
        html.push("<p class='chahao' name='delhistory'><img src='../../images/chahao.png' /></p>");
        html.push("</div>");
    }
    $("#searchhistorylist").html(html.join(""));
}
